package models

import (
	
	"github.com/astaxie/beego/orm"
	"github.com/pkg/errors"
)

type IpccAppInfo struct {
	Id          int
	AppId       string `orm:"column(appid)"`
	AppName     string
	CallAddress string
	Active      int
	Brand       string
	RingCheck   bool
}

func RegIpccAppManager() {
	orm.RegisterModel(new(IpccAppInfo))
}

func GetAllAppsFromId(pageSize, page, id int) (result *[]IpccAppInfo, err error) {

	o := orm.NewOrm()
	o.Using("default")

	result = &[]IpccAppInfo{}

	count, err := o.QueryTable(new(IpccAppInfo)).
		Filter("Active", 1).
		Filter("Id__gte", id).
		Limit(pageSize, pageSize*page).
		All(result)
	if err != nil {
		
		return result, err
	}

	if count == 0 {
		err = errors.New("no numbers found")
	}

	return result, err
}

func GetAppsByAppid(appId string) (result *IpccAppInfo, err error) {

	o := orm.NewOrm()
	o.Using("default")

	result = &IpccAppInfo{}

	err = o.QueryTable(new(IpccAppInfo)).
		Filter("Active", 1).
		Filter("AppId", appId).
		One(result)

	if err != nil {
		
		return result, err
	}

	return result, err
}
